package notificador.ip;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import jogoBolaoeArmazenador.rn.Usuario;

public class IPNotificacao {

	
	public void addNotificacao (Usuario user, String text) {
		
		Connection con = null;
		Statement stm = null;
		
		try {
			con = DriverManager.getConnection("jdbc:postgresql://localhost/senaonline","postgres", "bojador");
			stm = con.createStatement();	
			String query = "INSERT INTO	notificacao (login, notificacao) VALUES ('"+user.get_login()+"', '" + text + "')";
			stm.executeUpdate(query);
		} catch (SQLException e){
			e.printStackTrace();
		} finally{
			
			try{
				stm.close();
				con.close();
			}catch(Exception e){
				e.printStackTrace();
			}
			
		}
		
	}
	
	
	public void removeNotificacoes (Usuario user) {
		
		Connection con = null;
		Statement stm = null;
		
		try{
			con = DriverManager.getConnection("jdbc:postgresql://localhost/senaonline","postgres", "bojador");
			stm = con.createStatement();	
			String query = "DELETE FROM notificacao WHERE notificacao.login = '"+user.get_login()+"'";
			stm.executeUpdate(query);
		} catch(SQLException e){
			e.printStackTrace();
		} finally{
			try{
				stm.close();
				con.close();
			} catch(Exception e){
				e.printStackTrace();
			}
		}
	}

	public ArrayList<String> recuperaNotificacoes (Usuario user) {
		
		ArrayList<String> set = new ArrayList<String>();
		String mensagem; 
		
		Connection con = null;
		Statement stm = null;
		ResultSet rs = null;
		
		try{

			con = DriverManager.getConnection("jdbc:postgresql://localhost/senaonline","postgres", "bojador");
			stm = con.createStatement();
			rs = stm.executeQuery("SELECT * FROM notificacao WHERE notificacao.login = '"+user.get_login()+"'");
			while(rs.next()){
				mensagem = rs.getString("notificacao");
				set.add(mensagem);
			}
			
		}catch(SQLException e){
			e.printStackTrace();
		}finally{
			try{
				rs.close();
				stm.close();
				con.close();
			}catch(Exception e){
				e.printStackTrace();
			}
		}
		return set;
	}
}
